package com.example.koizzi.wuziqi;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by yls on 2018/6/13.
 */

public class ChessPanel extends View {
    private int MAX_Line = 10;//设置10行
    private int panelWidth;//棋盘宽度
    private float lineHeight;//棋盘单行行高
    private Paint paint = new Paint();

    private Bitmap mWhitePiece;//白子
    private Bitmap mBlackPiece;//黑子

    private final float radioPieceOfLineHeight = 3*1.0f/4;//棋子占行距的比例
    private boolean isWhite = true;//白子先手，轮到白子

    //用于存放已下棋子数据
    private ArrayList<Point> whiteArray = new ArrayList<>();
    private ArrayList<Point> blackArray = new ArrayList<>();

    private boolean isWin = false;
    private boolean isWhiteWin;

    public ChessPanel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        setBackgroundColor(0x5522dd22);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//计算布局大小
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);//获取棋盘宽
        int wMode = MeasureSpec.getMode(widthMeasureSpec);

        int height = MeasureSpec.getSize(heightMeasureSpec);//获取棋盘高
        int hMode = MeasureSpec.getMode(heightMeasureSpec);

        int lenth = Math.min(width, height);

        if (wMode == MeasureSpec.UNSPECIFIED) {//MeasureSpec.UNSPECIFIED表示未知大小
            lenth = height;
        } else if (hMode == MeasureSpec.UNSPECIFIED) {
            lenth = width;
        }

        setMeasuredDimension(lenth, lenth);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {//当View大小发生改变的时候会被系统自动回调
        super.onSizeChanged(w, h, oldw, oldh);
        panelWidth = w;
        lineHeight = panelWidth * 1.0f / MAX_Line;

        int pieceWidth = (int) (lineHeight*radioPieceOfLineHeight);
        mBlackPiece = Bitmap.createScaledBitmap(mBlackPiece,pieceWidth,pieceWidth,false);
        mWhitePiece = Bitmap.createScaledBitmap(mWhitePiece,pieceWidth,pieceWidth,false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBroad(canvas);//画出棋盘线
        drawPiece(canvas);//画出棋子
        gameOver();
        if (isWin){
            if (isWhiteWin){
                Toast.makeText(getContext(),"白棋赢了",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(getContext(),"黑棋赢了",Toast.LENGTH_SHORT).show();;
            }
        }
    }

    private void gameOver() {
        boolean wWin = checkWin(whiteArray);
        boolean bWin = checkWin(blackArray);
//        boolean
        if (wWin || bWin) {
            isWin = true;
            isWhiteWin = wWin;
        }

    }

    private boolean checkWin(ArrayList<Point> points) {
        for (Point p:points){
            int x = p.x;
            int y = p.y;

            boolean horizontalCheck = horizontalCheckFive(x,y,points);
            boolean verticalCheck = VerticalCheckFive(x,y,points);
            boolean leftDiaCheck = leftDiaCheckFive(x,y,points);
            boolean rightDiaCheck = rightDiaCheckFive(x,y,points);
            if (horizontalCheck || verticalCheck ||
                    leftDiaCheck || rightDiaCheck) {
                return true;
            }
        }
        return false;
    }

    private boolean rightDiaCheckFive(int x, int y, ArrayList<Point> points) {
        int count = 1;
        for (int i=1;i<5;i++) {
            if (points.contains(new Point(x-i, y-i))) {
                count++;
            }else {
                break;
            }
        }
        if (count == 5) {
            return true;
        }
        for (int i=1;i<5;i++) {
            if (points.contains(new Point(x+i, y+i))) {
                count++;
            }else {
                break;
            }
        }
        return false;
    }

    private boolean leftDiaCheckFive(int x, int y, ArrayList<Point> points) {
        int count = 1;
        for (int i=1;i<5;i++) {
            if (points.contains(new Point(x-i, y+i))) {
                count++;
            }else {
                break;
            }
        }
        if (count == 5) {
            return true;
        }
        for (int i=1;i<5;i++) {
            if (points.contains(new Point(x+i, y-i))) {
                count++;
            }else {
                break;
            }
        }
        return false;
    }

    private boolean VerticalCheckFive(int x, int y, ArrayList<Point> points) {
        int count = 1;
        for (int i=1;i<5;i++) {
            if (points.contains(new Point(x, y+i))) {
                count++;
            }else {
                break;
            }
        }
        if (count == 5) {
            return true;
        }
        for (int i=1;i<5;i++) {
            if (points.contains(new Point(x+i, y-i))) {
                count++;
            }else {
                break;
            }
        }
        return false;
    }

    private boolean horizontalCheckFive(int x, int y, ArrayList<Point> points) {
        int count = 1;
        for (int i=1;i<5;i++) {
            if (points.contains(new Point(x-i, y))) {
                count++;
            }else {
                break;
            }
        }
        if (count == 5) {
            return true;
        }
        for (int i=1;i<5;i++) {
            if (points.contains(new Point(x+i, y))) {
                count++;
            }else {
                break;
            }
        }
        return false;
    }

    private void drawPiece(Canvas canvas) {
        for (int i=0;i<whiteArray.size();i++) {
            Point wPoint = whiteArray.get(i);
            canvas.drawBitmap(mWhitePiece,
                    (wPoint.x + (1-radioPieceOfLineHeight)/2)*lineHeight,
                    (wPoint.y + (1-radioPieceOfLineHeight)/2)*lineHeight,null
                    );
        }
        for (int i = 0,n = blackArray.size();i < n;i++) {
            Point bPoint = blackArray.get(i);
            canvas.drawBitmap(mBlackPiece,
                    (bPoint.x + (1 -radioPieceOfLineHeight) / 2) * lineHeight,
                    (bPoint.y + (1 -radioPieceOfLineHeight) / 2) * lineHeight,null);
        }
    }

    private void drawBroad(Canvas canvas) {
        int w = panelWidth;
        float lheight = lineHeight;
        for (int i=0;i<MAX_Line;i++){
            int startx = (int) (lheight/2);
            int endx = (int) (w-lheight/2);
            int y = (int) ((0.5+i)*lheight);
            canvas.drawLine(startx, y, endx, y, paint);//画横线
            canvas.drawLine(y, startx, y, endx, paint);//画竖线
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_UP){
            int x = (int) event.getX();
            int y = (int) event.getY();
            Point p = getPoint(x,y);
            if (whiteArray.contains(p)
                    || blackArray.contains(p)) {
                return false;
            }

            if (isWhite) {
                whiteArray.add(p);
            } else {
                blackArray.add(p);
            }
            invalidate();
            isWhite = !isWhite;
            return true;
        }
        return true;
    }

    private Point getPoint(int x, int y) {
        return new Point((int)(x / lineHeight),(int)(y / lineHeight));
    }

    private void initPaint() {
        paint.setColor(0x44ff0000);
        paint.setAntiAlias(true);//抗锯齿
        paint.setDither(true);//防抖动
        paint.setStyle(Paint.Style.STROKE);//画笔样式，描边Stroke
        if (mWhitePiece == null) {
            mWhitePiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_w2);
        }
        if (mBlackPiece == null) {
            mBlackPiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_b1);
        }
    }

}
